package cn.yunhe.service.impl;

import cn.yunhe.dao.ExamPaperDao;
import cn.yunhe.dao.QuestionDao;
import cn.yunhe.entity.ExamPaper;
import cn.yunhe.entity.Question;
import cn.yunhe.service.ExamPaperService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.star.uno.RuntimeException;
import org.apache.ibatis.annotations.Insert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("examPaperService")
public class ExamPaperServiceImpl implements ExamPaperService {

    @Resource
    private ExamPaperDao examPaperDao;

    @Resource
    private QuestionDao questionDao;

    /**
     * 根据标题,分类,分页模糊查询现有试卷
     * @param cond
     * @return
     * author guohanyu
     */
    @Override
    public Page getPapersByFieldAndPointAndName(Map<String, Object> cond) {
        //先创建一个page对象
        Page page = new Page();
        //得到记录总数.付给page的total属性
        page.setTotal(examPaperDao.getPapersByFieldAndPointAndNameCount(cond));
        //获取每页大小
        int pageSize = Integer.parseInt(String.valueOf(cond.get("pageSize"))) ;
        page.setPageSize(pageSize);
        //获取当前页码
        int currPageNum =Integer.parseInt(String.valueOf(cond.get("pageNum"))) ;
        //计算出总页数
        page.setPageNum((int)(page.getTotal()/pageSize+(page.getTotal()%pageSize==0? 0:1)));

        System.out.println(cond.get("creator"));
        System.out.println(cond.get("createTime"));
        //启用分页
        page = PageHelper.startPage(currPageNum,pageSize);
        //查询列表
        List list = examPaperDao.getPapersByFieldAndPointAndName(cond);

        return page;
    }

    @Override
    public int getPaperCount() {
        return examPaperDao.getPaperCount();
    }


    /**
     * 发布考试,触发试题查询,插入等操作,需添加事务
     * @return
     * Map中包含数据(examPaper相关信息,questionCount,pointId集合)
     * 郭瀚钰
     */
    @Transactional(value="txManager1")
    @Override
    public boolean addExamPaper(Map map) throws RuntimeException {
        //定义一个标示
        boolean flag = true;
        //首先执行像试卷表插入试卷信息
        ExamPaper examPaper = (ExamPaper) map.get("examPaper");
        if (!examPaperDao.addExamPaper(examPaper)){
            return  flag = false;
        }
        //根据知识点从题库中获取试题
        //获取选择题
        System.out.println(map.get("pointList"));
        System.out.println(map.get("questionCount"));
        List<Question> questionList = questionDao.getSeveralQuestion(map);
        //获取判断题
        System.out.println(map.get("judgmentQuestionCount"));
        List<Question> judgmentQuestionList = questionDao.getSeveralJudgmentQuestion(map);
        //向试卷试题表插入选择试题
        for (Question ql : questionList) {
            map.put("questionId", ql.getId());
            map.put("examPaperId", examPaper.getId());
            if (!questionDao.addQuestionsToExamPaper(map)){
                return   flag = false;
            }
        }

        //向试卷试题表插入判断试题
        for (Question jql : judgmentQuestionList) {
            map.put("questionId", jql.getId());
            map.put("examPaperId", examPaper.getId());
            map.put("questionPoint",map.get("judgmentQuestionPoint"));
            System.out.println(map.get("questionPoint"));
            if (!questionDao.addQuestionsToExamPaper(map)){
                return   flag = false;
            }
        }

        return flag;
    }


    /**
     * 根据Id获取试卷信息
     * @param examPaperId
     * @return
     */
    @Override
    public ExamPaper getExamPaperById(Integer examPaperId) {
        return   examPaperDao.getExamPaperById(examPaperId);
    }
    /**
     * 根据试卷Id获取试题集合
     * @param examPaperId
     * @return
     */
    @Override
    public List<Question> getQuestionsByExamPaperId(Integer examPaperId) {
        return    questionDao.getQuestionsByExamPaperId(examPaperId);
    }

    /**
     * 根据传入试卷Id集合,批量对试卷和其下属试题删除,需添加事务
     * @param examPaperIdList
     * @return
     */
    @Transactional(value="txManager1")
    @Override
    public boolean delExamPaperById(List<Integer> examPaperIdList) throws RuntimeException {
        //定义一个标示
        boolean flag = true;
        System.out.println(examPaperIdList);
        if(!examPaperDao.delExamPaperById(examPaperIdList)){
            return flag = false;
        }
        if (!questionDao.delExamPapersQuestion(examPaperIdList)){
            return flag =  false;
        }
        return flag;
    }

    /**
     * 从试卷中移除试题(需添加事务)
     * @param map 封装examPaperId,questionId,questionTypeId
     * @return
     */
    @Transactional(value = "txManager1")
    @Override
    public boolean removeQuestionFromExamPaper(Map<String, Object> map) throws RuntimeException {
        boolean flag = true;
        //从试卷中删除题目
        if (!questionDao.removeQuestionFromExamPaper(map)){
            return flag = false;
        }
        //修改试卷信息
        ExamPaper examPaper =  examPaperDao.getExamPaperById((Integer)map.get("examPaperId"));
        if ((Integer)map.get("questionTypeId")==1){
            //题目类型是选择题,计算该题分数
            Integer num = examPaper.getChooseQuestionTotalPoint()/examPaper.getQuestionCount();
            //修改总分
            examPaper.setTotalPoint(examPaper.getTotalPoint() - num);
            //修改该类型题目分数
            examPaper.setChooseQuestionTotalPoint(examPaper.getChooseQuestionTotalPoint() - num);
            //修改该类型题目数目
            examPaper.setQuestionCount(examPaper.getQuestionCount() - 1);
        }else if ((Integer)map.get("questionTypeId")==3){
            //题目类型是判断题,计算该题分数
            Integer num = examPaper.getJudgmentQuestionTotalPoint()/examPaper.getJudgmentQuestionCount();
            //修改总分
            examPaper.setTotalPoint(examPaper.getTotalPoint() - num);
            //修改该类型题目分数
            examPaper.setJudgmentQuestionTotalPoint(examPaper.getJudgmentQuestionTotalPoint() - num);
            //修改该类型题目数目
            examPaper.setJudgmentQuestionCount(examPaper.getJudgmentQuestionCount() - 1);
        }
        //执行修改
        System.out.println(examPaper);
        if (!examPaperDao.removeQuestionFromExamPaper(examPaper)){
            return flag = false;
        }
        return flag;
    }

    /**
     * 添加人工试卷选题添加
     * @param map 封装试卷信息,题目集合
     * @return
     */
    @Transactional(value = "txManager1")
    @Override
    public boolean addArtificialExamPaper(Map<String, Object> map) {
        //定义一个标示
        boolean flag = true;
        //首先执行像试卷表插入试卷信息
        ExamPaper examPaper = (ExamPaper) map.get("examPaper");
        if (!examPaperDao.addExamPaper(examPaper)){
            return  flag = false;
        }
        //获取选择题
        List<Integer> chooseQuestionList = (List<Integer>) map.get("chooseQuestionList");
        List<Question> questionList = new ArrayList<>();
        if (chooseQuestionList.size()>0) {
            for (Integer cql : chooseQuestionList) {
                Question question = new Question(cql);
                questionList.add(question);
            }
        }
        //获取判断题
        System.out.println(map.get("judgmentQuestionCount"));
        List<Integer> judgementQuestionLists = (List<Integer>) map.get("judgementQuestionList");
        List<Question> judgmentQuestionList = new ArrayList<>();
        if (judgementQuestionLists.size()>0) {
            for (Integer jql : judgementQuestionLists) {
                Question question = new Question(jql);
                judgmentQuestionList.add(question);
            }
        }
        //向试卷试题表插入选择试题
        if (questionList.size()>0) {
            for (Question ql : questionList) {
                System.out.println();
                map.put("questionId", ql.getId());
                map.put("examPaperId", examPaper.getId());
                if (!questionDao.addQuestionsToExamPaper(map)) {
                    return flag = false;
                }
            }
        }

        //向试卷试题表插入判断试题
        if (judgmentQuestionList.size()>0) {
            for (Question jql : judgmentQuestionList) {
                System.out.println();
                map.put("questionId", jql.getId());
                map.put("examPaperId", examPaper.getId());
                map.put("questionPoint", map.get("judgmentQuestionPoint"));
                System.out.println(map.get("questionPoint"));
                if (!questionDao.addQuestionsToExamPaper(map)) {
                    return flag = false;
                }
            }
        }
        return flag;
    }


    public QuestionDao getQuestionDao() {
        return questionDao;
    }

    public void setQuestionDao(QuestionDao questionDao) {
        this.questionDao = questionDao;
    }

    public ExamPaperDao getExamPaperDao() {
        return examPaperDao;
    }

    public void setExamPaperDao(ExamPaperDao examPaperDao) {
        this.examPaperDao = examPaperDao;
    }
}
